<template>
  <div>
    <div class="main">
       <van-field
        class="input_text"
        readonly
        label="下级单位"
        v-model="form.EnterpriseName"
        name="AppointmentTypeName"
        is-link
        required
      ></van-field>
      <van-field label="最近修改人" v-model="form.EditChnName" name="ChnName" required>
      </van-field>
      <van-field label="年份" v-model="form.Year" name="ChnName" required>
      </van-field>
      <!-- <van-field class="input_text" label="最近修改日期" :value="gettimeS(form.EditDate)" name="ChnName" required>
      </van-field> -->
      <!-- <van-field label="文件名称" v-model="form.Name" name="Name" required>
      </van-field>

      <van-field label="签字人" v-model="form.ChnName" name="ChnName" required>
      </van-field> -->

      <FileUpload
        @CallBack="_fileUploadCb"
        :attachs="form.Attachs"
        :labelName="'签名文件'"
        :mustInput="true"
      />

      <div class="btn_submit">
        <van-button block type="info" @click="submit"> 提交 </van-button>
      </div>
    </div>
  </div>
</template>

<script>
import {
  initEntity,
  addSubmit,
  GetDetail,
} from "@/api/CompanyProductionSafety";
import { uploadAttach } from "@/api/common";
import FileUpload from "@/components/Attach/FileUpload";
import { mapGetters } from "vuex";

export default {
  components: {
    FileUpload,
  },
  data() {
    return {
      id: this.$route.query.id,
      radio: "1",
      selectList: [],
      form: {},
      filePopup: false,
      showAppointmentType: false,
    };
  },

  computed: {
    ...mapGetters(["baseUrl", "CompanyCode", "roles"]),
  },

  created() {
    if (this.$route.query.id != null) {
      (this.id = this.$route.query.id), this.getDetail();
    }

    console.log("id", this.$route.query.id);

    //this.getDistList();
  },
  activated() {
    // if (this.$route.params.certFile) {
    // 	this.fileList.push({
    // 		AttCode: this.$route.params.certFile.AttCode,
    // 		AttExt: this.$route.params.certFile.AttExt,
    // 		AttName: this.$route.params.certFile.AttName,
    // 	})
    // }
    //将该页面的id和新获取的id进行比对，如果不一致，说明页面应该进行数据刷新
    //重新请求数据
    // if (this.$route.params.userlist) {
    //   const userlist = this.$route.params.userlist;
    //   let EquManagerChnName = [];
    //   let EquManagerUserName = [];
    //   userlist.forEach((item) => {
    //     EquManagerChnName.push(item.name);
    //     EquManagerUserName.push(item.code);
    //   });
    //   this.form.EquManagerChnName = EquManagerChnName.join(",");
    //   this.form.EquManagerUserName = EquManagerUserName.join(",");
    // }
  },
  methods: {
    _fileUploadCb(attachs) {
      this.form.Attachs = attachs;
    },

    radioChange(names) {
      let select = this.form.AppointmentTypeList.filter(
        (item) => item.ID == names
      );
      this.form.AppointmentTypeName = select[0].Name;
      this.form.AppointmentType = select[0].ID;

      this.showAppointmentType = false;
    },

    getDetail(certId) {
      let params = { id: this.id };

      if (this.id == 0) {
        initEntity(params).then((res) => {
          // if (res.data.Attachs) {
          //   let Attachs = res.data.Attachs.split(";");
          //   console.log(Attachs, "Attachs");
          //   res.data.Attachs = JSON.parse(res.data.Attachs);
          // } else {
          //   res.data.Attachs = [];
          // }
          this.form = res.data;
        });
      } else {
        GetDetail(params).then((res) => {
          // if (res.data.Attachs) {
          //   let Attachs = res.data.Attachs.split(";");
          //   console.log(Attachs, "Attachs");
          //   res.data.Attachs = JSON.parse(res.data.Attachs);
          // } else {
          //   res.data.Attachs = [];
          // }
          this.form = res.data;
        });
      }
    },

    submit() {
      if (!this.form.Name) {
        return this.$toast("请输入文件名称");
      }

      if (!this.form.ChnName) {
        return this.$toast("请输入签字人");
      }

      if (!this.form.Attachs) {
        return this.$toast("请选择签名文件");
      } else {
        if (this.form.Attachs.length == 0) {
          return this.$toast("请选择签名文件");
        }
      }

      //      id	0
      // CompanyCode	00001
      // Name	文件名称
      // ChnName	签字人
      // Attachs	[{"AttCode":"CA21031900003","AttExt":".docx","AttName":"安全生产规章制度汇编-20210319-003"}]

      // if(this.form){
      let params = {
        Attachs: this.form.Attachs,
        id: this.form.ID,
        ChnName: this.form.ChnName,
        Name: this.form.Name,
        CompanyCode: this.CompanyCode,
      };
      if (this.id == 0) {
        addSubmit(params).then((res) => {
          this.$toast.success("新增成功");
          this.form = {
            ID: 0,
          };
          this.$router.go(-1);
          // setTimeout(() => {
          //   // this.$router.push("/Site_Appointment");
          //   this.$router.go(-1);
          // }, 0);
        });
      } else {
        addSubmit(params).then((res) => {
          this.$toast.success("保存成功");
          this.form = {
            ID: 0,
          };
          this.$router.go(-1);
          // setTimeout(() => {
          //   // this.$router.push("/Site_Appointment");
          //   this.$router.go(-1);
          // }, 500);
        });
      }
    },
  },
};
</script>

<style lang="less" >
.wrapper {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
}

.block {
  width: 120px;
  height: 120px;
  background-color: #fff;
}
</style>
<style lang="less" scoped>
.space-between {
  justify-content: space-between;
}
</style>
